//
//  RegisteredViewController.m
//  ZhiBoYuan
//
//  Created by apple on 2018/4/17.
//  Copyright © 2018年 apple. All rights reserved.
//

#import "RegisteredViewController.h"
#import "LogInViewController.h"
#import "TeaterRegistViewController.h"
#import "XIeYiViewController.h"

@interface RegisteredViewController ()
@property (weak, nonatomic) IBOutlet UITextField *phone;
@property (weak, nonatomic) IBOutlet UITextField *number;
@property (weak, nonatomic) IBOutlet UITextField *name;
@property (weak, nonatomic) IBOutlet UITextField *pasword;
@property (weak, nonatomic) IBOutlet UITextField *numberTwo;
@property (weak, nonatomic) IBOutlet UIButton *regist;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *hight;

@property (weak, nonatomic) IBOutlet UILabel *registerType;
@property (nonatomic, assign)int codeNum;

@property (nonatomic, copy)UIButton *numberButton;

@property (nonatomic,strong)UIButton *rightButton;
@property (weak, nonatomic) IBOutlet UIImageView *logoImage;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *logohight;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *logowidth;
@property (weak, nonatomic) IBOutlet UIButton *xieyiBtn;
@property (weak, nonatomic) IBOutlet UIButton *gouXuanBtn;

@end

@implementation RegisteredViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    _hight.constant = HeightConfig(40);
    
    _codeNum = 60;
    
    [self layoutUI];
    
    self.logohight.constant =  HeightConfig(57);
    
    self.logowidth.constant = WidthConfig(172);
    
    BTN_MSG_FUNC(_xieyiBtn, xieyiAction);
    BTN_MSG_FUNC(_gouXuanBtn, gouxuanAction);
    [_gouXuanBtn setBackgroundImage:[UIImage imageNamed:@"未选择"] forState:UIControlStateSelected];
    [_gouXuanBtn setBackgroundImage:[UIImage imageNamed:@"选择"] forState:0];
}

-(void)gouxuanAction{
    _gouXuanBtn.selected = !_gouXuanBtn.selected;
    NSLog(@"cnasbciaknls");
}

-(void)xieyiAction{
    
     XIeYiViewController *xieyiVC = [[XIeYiViewController alloc]init];
    xieyiVC.isComeFromLogin = NO;
    if ([self.registerType.text isEqualToString:@"当前为老师注册"]) {
        xieyiVC.isTeacher = YES;
    }else{
        xieyiVC.isTeacher = NO;
        
    }
    [self.navigationController pushViewController:xieyiVC animated:YES];
}

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    [_phone resignFirstResponder];
     [_number resignFirstResponder];
     [_name resignFirstResponder];
     [_pasword resignFirstResponder];
     [_numberTwo resignFirstResponder];
}

- (void)buttonSender:(UIButton *)sender {
    sender.selected = !sender.selected;
    if (sender.selected == YES) {
        self.registerType.text = @"当前为老师注册";
    }else{
        self.registerType.text = @"当前为学生注册";
        
    }
    
}


-(void)layoutUI{
    
    UIButton * button = [UIButton buttonWithType:(UIButtonTypeCustom)];
    
    //    [button setTintColor:ZhuTiColor];
    [button setTitle:@"教师注册" forState:(UIControlStateNormal)];
    [button setTitle:@"学生注册" forState:(UIControlStateSelected)];
    
    [button setTitleColor:ZhuTiColor forState:(UIControlStateNormal)];
    [button setTitleColor:ZhuTiColor forState:(UIControlStateSelected)];
    
    
    
    button.titleLabel.font = [UIFont systemFontOfSize:14];
    
    button.selected = NO;
    
    button.layer.borderWidth = 1;
    button.layer.borderColor = ZhuTiColor.CGColor;
    
    button.frame = CGRectMake(0, 0, 80, 20);
    
    [button addTarget:self action:@selector(buttonSender:) forControlEvents:(UIControlEventTouchUpInside)];
    
    _rightButton = button;
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:button];
    
    _hight.constant = SNRealValue(40);
    
//    _imageHight.constant = SNRealValue(120);
//    
//    _imLeft.constant = SNRealValue(50);
//    
//    _imRight.constant = SNRealValue(50);
    
    _phone.layer.masksToBounds = YES;
    _phone.layer.cornerRadius = 20;
    _phone.layer.borderWidth = 1;
    _phone.layer.borderColor = hui1Color.CGColor;
    
    UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 15, 1)];
    _phone.leftView = view;
    _phone.leftViewMode = UITextFieldViewModeAlways;
    
    
    _number.layer.masksToBounds = YES;
    _number.layer.cornerRadius = 20;
    _number.layer.borderWidth = 1;
    _number.layer.borderColor = hui1Color.CGColor;
    
    
    _numberButton = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 80, 40)];
    
    [_numberButton setTitle:@"获取验证码" forState:(UIControlStateNormal)];
    
    [_numberButton setTitleColor:[UIColor blackColor] forState:(UIControlStateNormal)];
    
    [_numberButton.titleLabel setFont:[UIFont systemFontOfSize:13]];
    
    BTN_MSG_FUNC(_numberButton, getNumberButton:);
    
    _number.rightView = _numberButton;
    
    [_number setRightViewMode:(UITextFieldViewModeAlways)];
    
    
    UIView *view2 = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 15, 1)];
    _number.leftView = view2;
    _number.leftViewMode = UITextFieldViewModeAlways;
    
    _name.layer.masksToBounds = YES;
    _name.layer.cornerRadius = 20;
    _name.layer.borderWidth = 1;
    _name.layer.borderColor = hui1Color.CGColor;
    
    
    UIView *view3 = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 15, 1)];
    _name.leftView = view3;
    _name.leftViewMode = UITextFieldViewModeAlways;
    
    
    _pasword.layer.masksToBounds = YES;
    _pasword.layer.cornerRadius = 20;
    _pasword.layer.borderWidth = 1;
    _pasword.layer.borderColor = hui1Color.CGColor;
    
    
    UIView *view4 = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 15, 1)];
    _pasword.leftView = view4;
    _pasword.leftViewMode = UITextFieldViewModeAlways;
    _pasword.secureTextEntry = YES;
    
    _numberTwo.layer.masksToBounds = YES;
    _numberTwo.layer.cornerRadius = 20;
    _numberTwo.layer.borderWidth = 1;
    _numberTwo.layer.borderColor = hui1Color.CGColor;
    
    
    UIView *view5 = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 15, 1)];
    _numberTwo.leftView = view5;
    _numberTwo.leftViewMode = UITextFieldViewModeAlways;
    
    _regist.layer.masksToBounds = YES;
    _regist.layer.cornerRadius = 20;
    
    
    BTN_MSG_FUNC(_regist,registSender:);
}


-(void)getNumberButton:(UIButton *)sender{
    if (_phone.text.length != 11) {
        [MBProgressHUD showTextOnly:self.view message:@"请输入正确的手机号"];
        return;
    }
    
    SGParameter *parameter  = [[SGParameter alloc] init];
    parameter.body = @{
                       @"phone":_phone.text,
                       @"type":@"register"
                       };
    
    [SGHttpTool POST:[NSString stringWithFormat:@"%@g=Client&m=Register&a=getSmsCode",HTTPP] parameter:parameter sucess:^(id responseObject) {
        NSLog(@"%@",responseObject);
        
        NSDictionary *dic = responseObject;
        if ([dic[@"status"]intValue] == 200) {
            [MBProgressHUD showTextOnly:self.view message:@"验证码发送成功" delay:3.0];
            
            [NSTimer scheduledTimerWithTimeInterval:1.0
                                             target:self
                                           selector:@selector(handleMaxShowTimer:)
                                           userInfo:nil
                                            repeats:YES];
            
        }else{
            [MBProgressHUD showTextOnly:self.view message:dic[@"msg"] delay:3.0];
        }
        
    } failure:^(NSError *error) {
        NSLog(@"%@",error);
        
    }];

  
}

#pragma mark  - 验证码倒计时
- (void)handleMaxShowTimer:(NSTimer *)timer{
    
    _codeNum--;
    [_numberButton setTitle:[NSString stringWithFormat:@"重新获取(%ld)",(long)_codeNum]   forState:UIControlStateNormal];
    _numberButton.userInteractionEnabled = NO;
    if (_codeNum == 0) {
        NSLog(@"验证码超时");
        //        _codeNum = @"fsdsa";
        [_numberButton setTitle:@"重新获取" forState:UIControlStateNormal];
        [timer invalidate];
        timer = nil;
        _codeNum = 60;
        _number.userInteractionEnabled = YES;
    }
    
}


-(void)registSender:(UIButton *)sender{

    if (_phone.text.length != 11) {
        [MBProgressHUD showTextOnly:self.view message:@"请输入正确的手机号"];
        return;
    }
    
    if (_number.text.length == 0) {
        [MBProgressHUD showTextOnly:self.view message:@"请输入验证码"];
        return;
    }
    
    if (_name.text.length == 0) {
        [MBProgressHUD showTextOnly:self.view message:@"请输入您的姓名"];
        return;
    }
    
    if (_pasword.text.length < 6 || _pasword.text.length > 20) {
        [MBProgressHUD showTextOnly:self.view message:@"请设置6-20位密码"];
        return;
    }
    
    if ([_numberTwo.text intValue] == 0 && _numberTwo.text.length != 0) {
        [MBProgressHUD showTextOnly:self.view message:@"请输入数字邀请码"];
        return;
    }
    
    if (_gouXuanBtn.selected) {
        [MBProgressHUD showTextOnly:self.view message:@"请先阅读用户协议"];
        return;
    }
    
    NSString *type = @"1";
    
    if (_rightButton.selected) {
        type = @"2";//老师
    }else{
        type = @"1";//学生
    }
    
    SGParameter *parameter  = [[SGParameter alloc] init];
    parameter.body = @{@"phone":_phone.text,
                       @"name":_name.text,
                       @"password":_pasword.text,
                       @"code":_number.text,
                       @"type":type,
                       @"invite_code":_numberTwo.text
                       };
    
    [SGHttpTool POST:[NSString stringWithFormat:@"%@g=Client&m=Register&a=regUser",HTTPP] parameter:parameter sucess:^(id responseObject) {
        NSLog(@"%@",responseObject);
        
        NSDictionary *dic = responseObject;
        
        if ([dic[@"status"]intValue] == 200) {
            
            UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"" message:@"注册成功" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil, nil];
            [alertView show];
            
            
            if (_rightButton.selected) {
                
                UserinfoModel *model = [ZYUserManager userInfo];
                
                model.token = [dic[@"data"][@"user_info"] valueForKey:@"token"];
                [ZYUserManager saveUserInfo:model];
                
//老师
                TeaterRegistViewController * vc = [[TeaterRegistViewController alloc]initWithNibName:@"TeaterRegistViewController" bundle:nil];
                
                [self.navigationController pushViewController:vc animated:YES];
                
            }else{
                LogInViewController *loginVC = [[LogInViewController alloc]init];
                [self.navigationController pushViewController:loginVC animated:YES];
                
            }
           
            
        }else{
            [MBProgressHUD showTextOnly:self.view message:dic[@"msg"] delay:2.0];
            
        }
        
        
    } failure:^(NSError *error) {
        NSLog(@"%@",error);
        
    }];
    
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
